package com.ds.lens.data.controller;

import com.ds.lens.data.service.LensMetricsTagsService;
import com.ds.lens.data.service.dao.entity.LensMetricsTags;
import com.ds.lens.data.vo.BaseOutVO;
import com.ds.lens.data.vo.enums.DataCommonVOMessageEnum;
import com.ds.lens.data.vo.factory.VOFactory;
import com.ds.lens.data.vo.in.User;
import com.ds.lens.data.vo.in.v2.metricstags.LensMetricsTagsInVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hill
 * @date 2019/11/6 下午2:32
 */
@RestController
@Api(value = "Lens Metrics-tags controller")
@RequestMapping(produces = {"application/json;charset=UTF-8"})
public class LensMetricsTagsController {

    @Autowired
    LensMetricsTagsService lensMetricsTagsService;

    @ApiOperation(value = "save Metrics tags")
    @PostMapping(value = "/lens/metricstags/save")
    public BaseOutVO saveMetricsTags(@RequestBody List<LensMetricsTagsInVO> metricsTagsInVOList) {
        List<LensMetricsTags> lensMetricsTagsList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(metricsTagsInVOList)){
            for(LensMetricsTagsInVO lensMetricsTagsInVO : metricsTagsInVOList){
                LensMetricsTags lensMetricsTags = new LensMetricsTags();
                BeanUtils.copyProperties(lensMetricsTagsInVO, lensMetricsTags);
                lensMetricsTagsList.add(lensMetricsTags);
            }

        }
        return VOFactory.getSuccessBaseOutVO(lensMetricsTagsService.batchSaveMetricsTags(lensMetricsTagsList));
    }



    @ApiOperation(value = "Get all Metrics tags")
    @GetMapping(value = "/lens/metricstags/getAllMetricsTags")
    public BaseOutVO getAllMetricsTags() {
        List<LensMetricsTags> lensMetricsTagsList = lensMetricsTagsService.getAllMetircsTags();
        if (null != lensMetricsTagsList) {
            return VOFactory.getSuccessBaseOutVO(lensMetricsTagsList);
        }
        return VOFactory.getBaseOutVO(DataCommonVOMessageEnum.COMMON_FAILED.getCode(), DataCommonVOMessageEnum.COMMON_FAILED.getMessage());
    }

    @ApiOperation(value = "getMetricNameByAppId")
    @PostMapping(value = "/lens/metricstags/getMetricNameByAppId")
    public BaseOutVO getMetricNameByAppId(@RequestBody LensMetricsTagsInVO lensMetricsTagsInVO,
                                             @RequestAttribute(value = "user", required = false) User user) {
        List<String> metricNameList = lensMetricsTagsService.getMetricNameByAppId(lensMetricsTagsInVO.getAppId());
        if (null != metricNameList) {
            return VOFactory.getSuccessBaseOutVO(metricNameList);
        }
        return VOFactory.getBaseOutVO(DataCommonVOMessageEnum.COMMON_FAILED.getCode(), DataCommonVOMessageEnum.COMMON_FAILED.getMessage());
    }

    @ApiOperation(value = "getMetricTagKeyByAppId")
    @PostMapping(value = "/lens/metricstags/getMetricTagKeyByAppId")
    public BaseOutVO getMetricTagKeyByAppId(@RequestBody LensMetricsTagsInVO lensMetricsTagsInVO,
                                          @RequestAttribute(value = "user", required = false) User user) {
        List<String> metricNameList = lensMetricsTagsService.getMetricTagKeyByAppId(lensMetricsTagsInVO.getAppId(), lensMetricsTagsInVO.getMetricName());
        if (null != metricNameList) {
            return VOFactory.getSuccessBaseOutVO(metricNameList);
        }
        return VOFactory.getBaseOutVO(DataCommonVOMessageEnum.COMMON_FAILED.getCode(), DataCommonVOMessageEnum.COMMON_FAILED.getMessage());
    }

    @ApiOperation(value = "getMetricTagKeyValueByAppId")
    @PostMapping(value = "/lens/metricstags/getMetricTagKeyValueByAppId")
    public BaseOutVO getMetricTagKeyValueByAppId(@RequestBody LensMetricsTagsInVO lensMetricsTagsInVO,
                                            @RequestAttribute(value = "user", required = false) User user) {
        List<String> metricNameList = lensMetricsTagsService.getMetricTagKeyValueByAppId(lensMetricsTagsInVO.getAppId(), lensMetricsTagsInVO.getMetricName());
        if (null != metricNameList) {
            return VOFactory.getSuccessBaseOutVO(metricNameList);
        }
        return VOFactory.getBaseOutVO(DataCommonVOMessageEnum.COMMON_FAILED.getCode(), DataCommonVOMessageEnum.COMMON_FAILED.getMessage());
    }

}
